package org.yys.seata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.yys.seata.entity.OrderEntity;
import org.yys.seata.feign.AccountClient;
import org.yys.seata.feign.StockClient;
import org.yys.seata.service.OrderService;
import org.yys.seata.mapper.OrderMapper;
import org.springframework.stereotype.Service;

/**
* @author A
* @description 针对表【t_order】的数据库操作Service实现
* @createDate 2023-12-05 16:17:53
*/
@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity>
    implements OrderService{

    @Autowired
    private AccountClient accountClient;

    @Autowired
    private StockClient stockClient;

    @Override
    @GlobalTransactional
    public void create(OrderEntity order) {
        order.setStatus(0);
        this.save(order);

        accountClient.decrease(order.getUserId(),order.getMoney());

        stockClient.decrease(order.getProductId(),order.getCount());

        order.setStatus(1);
        this.updateById(order);

        // 此处模拟错误
        int i=10/0;

    }
}




